#include <iostream>

using namespace std;

void function0(int a, int &b, int *c);
void function1(int a, int &b, int *c, int y, char ch);
void functionArray();
// 辅助
void function2(int a, int &c, int b);
void function3(int a, int b, int c, int &d);
void function4(long a, long b, long c);
void function5(int *a, int *b, int *c);
void function6(char a, char b, char c, int d,char f);

int main()
{
    int a = 10, b = 3;
    int *a3 = &a;
    int &a2 = a;

    a = 5;
    // int **b0 = &a3;
    // cout<<b0<<":"<<&b0<<":"<<*&b0<<endl;
    printf("原始地址\n");
    cout << "&a:" << &a << "\n&a2:" << &a2 << "\n&a3:" << &a3 << endl;
    printf("值传递\n");
    function0(a, a, &a);
    printf("引用传递\n");
    function0(a2, a2, &a2);
    printf("指针传递\n");
    function0(*a3, *a3, a3);
    printf("hello wirld\n");

    printf("指针传递的问题\n");
    function1(a, a2, a3, 45, 'k');
    function2(a, a, a);
    function3(a, a, a, a);
    function4(a, a, a);
    function5(a3, a3, a3);
    function6('a', 'b', 'c', 10,'d ');
    cout << "结局" << endl;

    functionArray();
    cout << "&a:" << &a << "\n&a2:" << &a2 << "\n&a3:" << &a3 << endl;

    // function0(b, b, &b);

    int x = 100;
    cout << "x:" << *&x << endl;
    *(int *)100 = 50;
    cout << "x:" << x << endl;
    return 0;
};
void function0(int a, int &b, int *c)
{
    cout << a << ":" << &a << ":" << endl;
    cout << b << ":" << &b << ":" << endl;
    cout << c << ":" << &c << ":" << *c << endl;
};
void function1(int a, int &b, int *c, int d, char ch)
{
    // 改变不了函数形参的地址，只能变更它的值
    cout << "a:" << &a << "\tb:" << &b << "\tc:" << c << "\t&c" << &c << "\t n:" << &d << endl;
    int x = 112;
    int y = 45;
    int *z = &x;
    b = x;
    c = z;
    cout << "a:" << &a << "\tb:" << &b << "\tc:" << c << "\t&c" << &c << endl; /*  */

    /*形参（包括引用地址8个长度，尽管类型不同, 或有char有变化*/ 
    printf("函数中局部变量的内存地址：start\n");
    cout << "x:" << &x << "+" << sizeof(x) << endl
         << "y:" << &y << "+" << sizeof(y) << endl
         << "z:" << &z << "+" << sizeof(z) << endl;
    printf("函数中局部变量的内存地址：finish\n");

    printf("函数中形参的内存地址：start\n");

    cout << &a << "+" << sizeof(a) << endl
         << &b << "+" << sizeof(b) << endl
         << &c << "+" << sizeof(c) << endl
         << &d << "+" << sizeof(d) << endl
         << (void *)&ch << "+" << sizeof(ch) << endl;
    printf("函数中形参的内存地址：end\n");
};
void function2(int a, int &b, int c)
{
    printf("新参引用的地址 start \n");
    cout << &a << "+" << sizeof(a) << endl
         << &b << "+" << sizeof(b) << endl
         << &c << "+" << sizeof(c) << endl;
    printf("新参引用的地址 finish\n");
}
void function3(int a, int b, int c, int &d)
{
    printf("新参引用的地址 start \n");
    cout << &a << "+" << sizeof(a) << endl
         << &b << "+" << sizeof(b) << endl
         << &c << "+" << sizeof(c) << endl
         << &d << "+" << sizeof(d) << endl;
    printf("新参引用的地址 finish\n");
}
void function4(long a, long b, long c)
{
    printf("新参引用的地址 start \n");
    cout << &a << "+" << sizeof(a) << endl
         << &b << "+" << sizeof(b) << endl
         << &c << "+" << sizeof(c) << endl;
    printf("新参引用的地址 finish\n");
    cout << sizeof(int *) << endl;
}
void function5(int *a, int *b, int *c)
{
    printf("新参引用的地址 start \n");
    cout << &a << "+" << sizeof(a) << endl
         << &b << "+" << sizeof(b) << endl
         << &c << "+" << sizeof(c) << endl;
    printf("新参引用的地址 finish\n");
    cout << sizeof(int *) << endl;
}
void function6(char a, char b, char c, int d, char f)
{
    printf("char 参引用的地址 start \n");
    cout << (void *)&a << "+" << sizeof(a) << endl
         << (void *)&b << "+" << sizeof(b) << endl
         << (void *)&c << "+" << sizeof(c) << endl
         << &d << "+" << sizeof(d) << endl
         << (void *)&f << "+" << sizeof(f) << endl;
    printf("char参引用的地址 finish\n");
}

void functionArray()
{
    // 局部变量地址大>小分配
    cout << "double数组" << endl; /*  */
    double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
    cout << sizeof(balance) << endl
         << &balance << endl
         << &balance[0] << endl
         << &balance[1] << endl
         << &balance[2] << endl
         << &balance[3] << endl
         << &balance[4] << endl;
    cout << "double" << endl;
    double a = 9.0;
    double a2 = 9.0;
    cout << &a << "+" << sizeof(a) << endl;
    cout << &a2 << "+" << sizeof(a2) << endl;
    cout << "char" << endl;
    char b = 'a';
    char *ptr = &b;
    cout << (void *)&b << "+" << sizeof(b) << endl;
    cout << &ptr << "+" << sizeof(ptr) << endl;
    cout << "char数组" << endl;
    char c[5] = {'a', 'f', 'f'};
    cout << sizeof(c) << endl
         << &c << endl
         << &c[0] << endl
         << &c[1] << endl
         << &c[2] << endl;
    cout << "string " << endl;
    string s = "sd";
    cout << sizeof(s) << endl
         << &s << endl;

    cout << "int " << endl;
    int x = 10;
    int y = 20;
    cout << &x << endl
         << &y << endl;

    cout << "int数组" << endl;
    int intArray[5] = {1000, 2, 3, 7, 50};
    cout << sizeof(intArray) << endl
         << &intArray << endl
         << &intArray[0] << endl
         << &intArray[1] << endl
         << &intArray[2] << endl
         << &intArray[3] << endl
         << &intArray[4] << endl;
}

